﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using UCL.Powersource.Analysis;

namespace UCL.Powersource.Statistics
{
    /// <summary>
    /// Class of Reopened tasks
    /// </summary>
    [Export(typeof(IStatistic))]
    [DataContract(IsReference = true)]
    class TasksReopened : WIStateStatisticsBase
    {
        [DataMember]
        private readonly string _QueryString = "Select [ID], [Iteration Path], [State] From WorkItems Where [Work Item Type] = @WIType AND ever [State] = @WasEver AsOf @AsOf";     // Declaration of the _QueryString which returns work item ID, iteration path and state

        protected override string QueryString { get { return _QueryString; } }

        [DataMember]
        private readonly string _WIType = "Task";       // Declaration of Task

        protected override string WIType { get { return _WIType; } }

        [DataMember]
        private readonly string _State = "In Progress";     // State of the work item is In Progress

        protected override string State { get { return _State; } }

        [DataMember]
        private readonly string _WasEver = "Done";      // State of the work item which was ever Done

        protected override string WasEver { get { return _WasEver; } }

        [ExcludeFromCodeCoverage]
        public override string Label        // Return the title as "Tasks Reopened"
        {
            get { return "Tasks Reopened"; }
        }

        [ExcludeFromCodeCoverage]
        public override string Description      // Return the Description as "Number of Tasks reopened in an iteration"
        {
            get { return "Number of Tasks reopened in an iteration"; }
        }

        [ExcludeFromCodeCoverage]
        public override string Name     // Return Name as a "TasksReopened"
        {
            get { return "TasksReopened"; }
        }

        public static readonly Guid CN_Statistic_Guid = new Guid("3AF3E6FD-BFFB-4088-9BC4-8E7FF67FD7A2");
        [ExcludeFromCodeCoverage]
        public override Guid Statistic_Guid
        {
            get { return CN_Statistic_Guid; }
        }

        protected override double? CalculateFor(ITfsTeamProjectConfiguration teamProjConfig, ITimeFrame timeFrame)
        {
            if (StatisticsUtilities.ValidIterationPath(teamProjConfig, timeFrame))
                return CalculateStatistic(teamProjConfig, timeFrame, QueryString);

            return null;
        }

    }
}


